﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YTSK.Vouch
{
    public class VouchData : VouchInit
    {

        public VouchData(string cvouchtype, Control c) :
            base(cvouchtype, c)
        {
        }

        #region 显示单据头数据
        public void DisplayDataForm(params  Dictionary<string,object>[] formval)
        {
            var c = this;
            var vouchhead = _vouchs.Where(x => x.iHead == 1).ToList();
            for (int i = 0; i < vouchhead.Count(); i++)
            {
                var cvou = vouchhead[i];
                var t = _main.Controls.Find(cvou.FieldCode, true);
                if (t.Count() > 0)
                {
                    var t0 = t[0];
                    if (t0.GetType() == typeof(TextBox))
                    { t0.Text = ""; }
                    if (t0.GetType() == typeof(DateTimePicker))
                    { ((DateTimePicker)t0).Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); }
                }
            }

            #region 显示单据头信息
            foreach (Dictionary<string, object> val in formval)
            {
                if (val == null) { continue; }
                foreach (var k in val)
                {
                    var v = k.Value.ObjToString();
                    if (v.Length > 0)
                    {
                        var t =_main.Controls.Find(k.Key, true);
                        if (t.Count() > 0)
                        {
                            var t0 = t[0];
                            if (t0.GetType() == typeof(ComboBox))
                            {
                                var cb = t0 as ComboBox;
                                for (var n = 0; n < cb.Items.Count; n++)
                                {
                                    var obj = (System.Data.DataRowView)cb.Items[n];
                                    var ss = obj["key"].ObjToString();
                                    if (obj["key"].ObjToString() == v)
                                    {
                                        cb.SelectedIndex = n;
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                if (t0.GetType() == typeof(DateTimePicker))
                                {
                                    if (v.ObjToDate().Year < 200)
                                    {
                                        continue ;
                                    }
                                }
                                t0.Text = v;
                            }
                        }
                    }
                }
            }
            #endregion
        }
        #endregion

        #region 显示表格数据
        public void Displaydata(List<Dictionary<string, object>> l_rds, bool bclarePre)
        {
            var c = this;
            #region 单据体
            if (bclarePre)
            { _dg.Rows.Clear(); }
            var ioldrow = _dg.Rows.Count;
            _dg.RowCount = ioldrow + l_rds.Count;
            for (int i = 0; i < l_rds.Count; i++)
            {
                var t0 = l_rds[i];
                var dgr = _dg.Rows[ioldrow + i];
                foreach (var k in t0)
                {
                    var v = k.Value.ObjToString();
                    if (v.Length > 0)
                    {
                        if (_dg.Columns.Contains(k.Key))
                        { dgr.Cells[k.Key].Value = v; }
                    }
                }
            }
            _dg.ReadOnly = false;
            for (int i = 0; i < _dg.Columns.Count; i++)
            {
                var dgc = _dg.Columns[i];
                var colname = dgc.Name;
                var t = _vouchs.FirstOrDefault(s => s.iHead == 2 && s.FieldCode == colname);
                if (t != null)
                {
                    if (t.bModify.ObjToInt() == 0) { dgc.ReadOnly = true; }   
                    else { dgc.ReadOnly = false; }
                }
            }
            base.sumcolAll();
            #endregion
        }
        #endregion



    }
}
